#include "func.h"

void save(link *head)
{
    FILE *fp;
    if ((fp = fopen("data.txt", "wb+")) == NULL)
    {
        perror("文件打开失败！");
        return;
    }

    link *tmp = head->next;
    link *ptr = tmp;
    while (tmp != NULL)
    {
        if (fwrite(&(tmp->p), sizeof(con), 1, fp) < 0)
        {
            printf("数据写入失败！\n");
            return;
        }     
        free(ptr);
        tmp = tmp->next;
        ptr = tmp;
    }
    free(head);
    fclose(fp);
    fp = NULL;
    printf("联系人保存成功！程序退出成功！\n");
}